//
//  ios_BitMapFontManager.cpp
//  Prism
//
//  Created by Bas du Pre on 24-05-11.
//  Copyright 2011 UU. All rights reserved.
//
/*
#include "sdl_BitMapFontManager.h"
#include "File.h"

#if PRISM_OS_IOS

using namespace Prism;

// Allocating and initializing Director's
// static data member.  The pointer is being
// allocated - not the object inself.
IOS_BitMapFontManager *IOS_BitMapFontManager::s_instance = 0;

IOS_BitMapFontManager *IOS_BitMapFontManager::instance() {
    if (!s_instance) s_instance = new IOS_BitMapFontManager;
    return s_instance;
}

IOS_BitMapFontManager::IOS_BitMapFontManager() {
    refresh();
}

IOS_BitMapFontManager::~IOS_BitMapFontManager() {
    for (unsigned int i = 0; i < bitMapFonts.length(); i++) {
        delete bitMapFonts[i];
    }
}

IOS_BitMapFontManager *Prism::bitMapFontManager() { return IOS_BitMapFontManager::instance(); }

IOS_BitMapFontManager::BitMapFont* IOS_BitMapFontManager::addBitMapFont(Prism::String font) {
    BitMapFont *bmFont = 0;
    for (unsigned int i = 0; i < bitMapFonts.length(); i++) {
        if (bitMapFonts[i]->font == font) {
            bmFont = bitMapFonts[i];
            break;
        }
    }
    if (bmFont == 0) {
        bmFont = new BitMapFont();
        bmFont->font = font;
        bitMapFonts.pushBack(bmFont);
    }
    return bmFont;
}

void IOS_BitMapFontManager::addBitMapFont(Prism::String font, PFloat size) {
    BitMapFont *bmFont = addBitMapFont(font);
    bmFont->sizes.pushBack(size);
}

void IOS_BitMapFontManager::refresh() {
    bitMapFonts.removeAll();
    NSString *bundleRoot = [NSString stringWithUTF8String: File::resourcesPath()];
    NSArray *dirContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:bundleRoot error:nil];
    //NSArray *onlyJPGs = [dirContents filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"self ENDSWITH '.jpg'"]];
    for (NSString *tString in dirContents) {
        if ([tString hasSuffix:@".fnt"]) {
            tString = [tString substringToIndex:[tString length] - 4];
            NSRange range = [tString rangeOfString: @"@"];
            if (range.location != NSNotFound)
            {
                NSString *font = [tString substringToIndex:range.location];
                NSString *strSize = [tString substringFromIndex:range.location + 1];
                PFloat size = [strSize floatValue];
                PRISM_LOGINFO_QUIET("Adding font '%s' (size %f)", [font cStringUsingEncoding:[NSString defaultCStringEncoding]], size);
                addBitMapFont([font cStringUsingEncoding:[NSString defaultCStringEncoding]], size);
            } else {
                PRISM_LOGINFO_QUIET("Adding font '%s' (size unknown)", [tString cStringUsingEncoding:[NSString defaultCStringEncoding]]);
                addBitMapFont([tString cStringUsingEncoding:[NSString defaultCStringEncoding]]);
            }
        }
    }
    
    return;
}

IOS_BitMapFontManager::BitMapFontSettings IOS_BitMapFontManager::bitMapFontForFontAndSize(String font, PFloat size) {
    if (bitMapFonts.length() == 0) {
        PRISM_ERROR("No fonts installed.");
        return BitMapFontSettings();
    }
    for (unsigned int i = 0; i < bitMapFonts.length(); i++) {
        if (bitMapFonts[i]->font == font) {
            BitMapFontSettings settings;
            settings.filename = bitMapFonts[i]->font;
            if (bitMapFonts[i]->sizes.length() == 0) {
                settings.filename += ".fnt";
                settings.scale = -1;
                return settings;
            }
            PFloat bestSize = -1;
            for (unsigned int j = 0; j < bitMapFonts[i]->sizes.length(); j++) {
                PFloat val = bitMapFonts[i]->sizes[j];
                if (bestSize < size && bestSize < val) {
                    bestSize = val;
                } else if (bestSize > size && val < bestSize && val > size) {
                    bestSize = val;
                }
            }
            settings.scale = size / bestSize;
            settings.filename += String("@") + String(bestSize) + ".fnt";
            return settings;
        }
    }
    PRISM_LOGERROR("Font '%@' does not exist. Defaulting to first known font.", &font);
    BitMapFontSettings settings;
    settings.filename = bitMapFonts[0]->font;
    if (bitMapFonts[0]->sizes.length() > 0)
        settings.filename += String("@") + String(bitMapFonts[0]->sizes[0]);
    settings.filename += ".fnt";
    return settings;
}

#endif
 */
